﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Microsoft.Win32;

namespace AutoMeasureV2._1
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            excelSelect.openEvent += excelSelect_openEvent;
            excelSelect.helpEvent += excelSelect_helpEvent;
            excelSelect.mesEvent += excelSelect_mesEvent;
            excelSelect.exitEvent += excelSelect_exitEvent;
        }

        fileHandle _fileHandle = new fileHandle();
        creatXml _creatXml = new creatXml();
        
        #region 右上三个图标操作
        private void Window_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            try
            {
                this.DragMove();
            }
            catch (Exception ex) { MessageBox.Show(ex.ToString()); }
        }

        private void Image_MouseLeftButtonDown_min(object sender, MouseButtonEventArgs e)
        {
            this.WindowState = System.Windows.WindowState.Minimized;
        }

        private void Image_MouseLeftButtonDown_max(object sender, MouseButtonEventArgs e)
        {
            this.WindowState = this.WindowState == System.Windows.WindowState.Normal ? System.Windows.WindowState.Maximized : System.Windows.WindowState.Normal;
        }

        private void Image_MouseLeftButtonDown_close(object sender, MouseButtonEventArgs e)
        {
            this.Close();
        }
        #endregion

        #region 按钮状态设置

        /// <summary>
        /// 修改按钮设定
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void resetButton_Click(object sender, RoutedEventArgs e)
        {
            excelSelect.Visibility = Visibility.Visible;

            //三个用户控件的状态设定
            tableUC.Visibility = Visibility.Hidden;
            stepUC.Visibility = Visibility.Hidden;
            chartUC.Visibility = Visibility.Hidden;

        }

        /// <summary>
        /// 确定按钮设定
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void confirmButton_Click(object sender, RoutedEventArgs e)
        {
            _fileHandle.xmlInfo(excelSelect.listComboBox.SelectedValue.ToString());
            stepUC.Handle = _fileHandle;
            chartUC.Handle = _fileHandle;

            #region 状态设置
            //三个用户控件的状态设定
            tableUC.Visibility = Visibility.Hidden;
            stepUC.Visibility = Visibility.Hidden;
            chartUC.Visibility = Visibility.Visible;

            //三个选择钮的状态设定
            radioButton1.IsChecked = false;
            radioButton2.IsChecked = true;
            radioButton3.IsChecked = false;

            radioButton1.Visibility = Visibility.Collapsed;
            radioButton2.Visibility = Visibility.Visible;
            radioButton3.Visibility = Visibility.Visible;

            ///四个按钮状态
            resetButton.Visibility = Visibility.Collapsed;
            confirmButton.Visibility = Visibility.Collapsed;
            backButton.Visibility = Visibility.Visible;
            saveButton.Visibility = Visibility.Visible;
            #endregion
        }

        /// <summary>
        /// 返回按钮设定
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void backButton_Click(object sender, RoutedEventArgs e)
        {
            //三个用户控件的状态设定
            tableUC.Visibility = Visibility.Visible;
            stepUC.Visibility = Visibility.Hidden;
            chartUC.Visibility = Visibility.Hidden;

            //四个按钮设定
            resetButton.Visibility = Visibility.Visible;
            confirmButton.Visibility = Visibility.Visible;
            backButton.Visibility = Visibility.Collapsed;
            saveButton.Visibility = Visibility.Collapsed;

            //三个选择钮的状态设定
            radioButton1.IsChecked = true;
            radioButton2.IsChecked = false;
            radioButton3.IsChecked = false;
            radioButton1.Visibility = Visibility.Visible;
            radioButton2.Visibility = Visibility.Collapsed;
            radioButton3.Visibility = Visibility.Collapsed;
        }

        /// <summary>
        /// 保存按钮设定
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void saveButton_Click(object sender, RoutedEventArgs e)
        {
            SaveFileDialog saveFileDialog = new SaveFileDialog();
            saveFileDialog.Filter = "XML(*.xml)|*.xml";
            saveFileDialog.AddExtension = true; //是否自动添加扩展名
            saveFileDialog.OverwritePrompt = true; //文件已存在是否提示覆盖
            saveFileDialog.CheckPathExists = true; //提示输入的文件名无效
            if ((bool)(saveFileDialog.ShowDialog()))
            {
                try
                {
                    string xmlPath = saveFileDialog.FileName;
                    _creatXml.writeXml(xmlPath ,_fileHandle );
                    MessageBox.Show("文件保存成功");
                }
                catch (Exception ex)
                {
                    MessageBox.Show("导出失败："+ex.Message );
                }
            }

        }



        /// <summary>
        /// 列表信息选择按钮设定
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void radioButton1_Checked(object sender, RoutedEventArgs e)
        {
            //三个用户控件的状态设定
            tableUC.Visibility = Visibility.Visible;
            chartUC.Visibility = Visibility.Hidden;
            stepUC.Visibility = Visibility.Hidden;
        }

        /// <summary>
        /// 工谱图例选择按钮设定
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void radioButton2_Checked(object sender, RoutedEventArgs e)
        {
            //三个用户控件的状态设定
            tableUC.Visibility = Visibility.Hidden;
            chartUC.Visibility = Visibility.Visible;
            stepUC.Visibility = Visibility.Hidden;
        }

        /// <summary>
        /// 工步序列选择按钮设定
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void radioButton3_Checked(object sender, RoutedEventArgs e)
        {
            //三个用户控件的状态设定
            tableUC.Visibility = Visibility.Hidden;
            chartUC.Visibility = Visibility.Hidden;
            stepUC.Visibility = Visibility.Visible;
        }

        #endregion


        #region 资源列表设置

        void excelSelect_openEvent(object sender, EventArgs e)
        {
            Microsoft.Win32.OpenFileDialog dlg = new Microsoft.Win32.OpenFileDialog();
            dlg.RestoreDirectory = true;
            dlg.Filter = "Excel文件(*.xlsx)|*.xlsx;*.xls|所有文件(*.*)|*.*";

            if (dlg.ShowDialog() == true)
            {
                excelSelect.addressTextBox.Text = dlg.FileName;
                _fileHandle.ExceltoDataTable(dlg.FileName);
                tableUC.Handle = _fileHandle;

                excelSelect.listComboBox.ItemsSource = _fileHandle.tableName;
                excelSelect.listComboBox.SelectedValuePath = "Value";
                excelSelect.listComboBox.DisplayMemberPath = "Value";
            }
        }

        void excelSelect_mesEvent(object sender, EventArgs e)
        {
            MessageBand _band = new MessageBand();
            _band.Show();
        }

        void excelSelect_helpEvent(object sender, EventArgs e)
        {

        }

        void excelSelect_exitEvent(object sender, EventArgs e)
        {
            try
            {
                tableUC.setName(excelSelect.listComboBox.SelectedValue.ToString());

                //三个用户控件的状态设定
                tableUC.Visibility = Visibility.Visible;
                stepUC.Visibility = Visibility.Hidden;
                chartUC.Visibility = Visibility.Hidden;

                //三个选择钮的状态设定
                radioButton1.IsChecked = true;
                radioButton2.IsChecked = false;
                radioButton3.IsChecked = false;
            }
            catch
            {
                MessageBox.Show("导入失败：导入对象错误");
            }
        }

        #endregion

    }
}
